import { _decorator, Component, Label, Node, ProgressBar } from 'cc';
import { DataManager } from '../DataManager';
const { ccclass, property } = _decorator;

@ccclass('RoundInfoController')
export class RoundInfoController extends Component {
  @property(ProgressBar)
  progressBar: ProgressBar | null = null;
  @property(Label)
  roundLabel: Label | null = null;
  @property(Label)
  timeLabel: Label | null = null;
  start() {}
  open() {
    this.node.active = true;
  }
  close() {
    this.node.active = false;
  }

  update(deltaTime: number) {
    const { gameData } = DataManager.instance;
    if (gameData) {
      const { roundStartTime, roundEndTime, round } = gameData;
      this.roundLabel.string = '第' + round + '回合';
      const now = Date.now();
      if (roundStartTime <= now && now <= roundEndTime) {
        this.progressBar.progress =
          (roundEndTime - now) / (roundEndTime - roundStartTime);
      }
      this.timeLabel.string =
        Math.max(0, (roundEndTime - now) / 1000).toFixed(0) + 's';
    }
  }
}
